#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>


int main()
{
  pid_t id = fork();
  if(id<0){
    perror("fork error!\n");
    return 1;
  }
  else if(id == 0){
    //child
    int count = 0;
    while(count<5){
      printf("child pid: %d ppid: %d is running ...\n", getpid(), getppid());
      sleep(1);
      count++;
    }
    exit(123);
  }
  else{
  //father
  printf("father wait before!\n");
  int status = 0;
  pid_t ret = waitpid(id, &status, 0);
  //pid_t ret = wait(NULL);//ret id 
  
  if(ret>=0){
    printf("wait success! ret = %d\n", ret);
    printf("exit status: %d\n", status);
    //printf("child exit code : %d\n",( status>>8)&0xFF);//child exit code 
    //printf("child get signal: %d\n", status&0x7F);
    printf("child exit code : %d\n",WEXITSTATUS(status));//child exit code 
    printf("child get signal: %d\n", WIFEXITED(status));
  }
  else{
    printf("wait failed!\n");
  }
  printf("father wait after!\n");
  }
}
